package jxtend.processor.eclipse.visitor.replace;

import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;

@SuppressWarnings("restriction")
//TODO: rename to "AllocationExpressionTypeReplacer"
public class QualifiedAllocationExpressionTypeReplacer implements
		ASTNodeReplaceHandler {

	@Override
	public boolean replaceNode(ASTNode parentNode, ASTNode originalNode, ASTNode newNode) {
		if (!(parentNode instanceof AllocationExpression)
				|| (!(originalNode instanceof TypeReference)
				|| !(newNode instanceof TypeReference))) {
			return false;
		}

		AllocationExpression allocationExpression = (AllocationExpression) parentNode;

		if (allocationExpression.type != originalNode) {
			throw new IllegalStateException(
					"Could not replace type, it's not the same as the type of the AllocationExpression");
		}

		TypeReference orginalType = (TypeReference) originalNode;

		allocationExpression.type = (TypeReference) newNode;

		if (allocationExpression.arguments == null) {
			allocationExpression.arguments = new Expression[0];
		}

		if (Thread.currentThread().getName().equals("org.eclipse.jdt.internal.ui.text.JavaReconciler")) {
			TypeReference newType = (TypeReference) newNode;

//			ReferenceBinding originalTypeReference = (orginalType.resolvedType instanceof ReferenceBinding)
//					? (ReferenceBinding) orginalType.resolvedType : null;
//			ReferenceBinding newTypeReference =
//				(newType.resolvedType instanceof ReferenceBinding) ? (ReferenceBinding) newType.resolvedType : null;
//
//			boolean addUnusedFieldWithOriginalType = (originalTypeReference != null)
//				&& EclipseProcessorUtils.isValidTypeReference(originalTypeReference) && (newTypeReference != null)
//				&& EclipseProcessorUtils.isValidTypeReference(newTypeReference)
//				&& (originalTypeReference != newTypeReference);

//			TypeDeclaration anonymousType = qualifiedAllocationExpression.anonymousType;

//			if (addUnusedFieldWithOriginalType) {
//				FieldDeclaration unusedFieldWithOriginalType =
//					new FieldDeclaration("_$".toCharArray(), orginalType.sourceStart, orginalType.sourceEnd);
//				unusedFieldWithOriginalType.type = orginalType;
//
//				SingleTypeReference suppressWarningsAnnotationTypeReference = new SingleTypeReference(
//						"SuppressWarnings".toCharArray(),
//						EclipseProcessorUtils.encodePos(anonymousType.bodyStart + 1, anonymousType.bodyStart + 1));
//
//				SingleMemberAnnotation suppressWarningsAnnotation = new SingleMemberAnnotation(
//						suppressWarningsAnnotationTypeReference, anonymousType.bodyStart + 2);
//				suppressWarningsAnnotation.sourceEnd = anonymousType.bodyStart + 2;
//				suppressWarningsAnnotation.statementEnd = anonymousType.bodyStart + 2;
//				suppressWarningsAnnotation.declarationSourceEnd = anonymousType.bodyStart + 2;
//
//				StringLiteral uncheckedLiteral = new StringLiteral(
//						"unused".toCharArray(), anonymousType.bodyStart + 3, anonymousType.bodyStart + 3, 0);
//				uncheckedLiteral.constant = Constant.NotAConstant;
//				suppressWarningsAnnotation.memberValue = uncheckedLiteral;
//
//				unusedFieldWithOriginalType.annotations = EclipseProcessorUtils.appendToArray(
//						unusedFieldWithOriginalType.annotations, Annotation.class, suppressWarningsAnnotation);
//
//				qualifiedAllocationExpression.anonymousType.fields = EclipseProcessorUtils.appendToArray(
//						qualifiedAllocationExpression.anonymousType.fields, FieldDeclaration.class,
//						unusedFieldWithOriginalType);
//			}
		}

		return true;
	}
}
